import { get, post, put, del } from "./api";

export const jobApplicationService = {
  /**
   * 获取所有求职申请
   * @param {Object} params - 分页参数
   * @returns {Promise<Object>} - 返回求职申请列表和分页信息
   */
  async getJobApplications(params = { page: 1, pageSize: 10 }) {
    try {
      const response = await get("job-applications", params);
      console.log("Job applications response:", response); // 添加日志

      // 处理不同的响应格式
      if (Array.isArray(response)) {
        // 如果返回的是数组，直接使用
        return {
          items: response,
          total: response.length,
          totalPages: Math.ceil(response.length / params.pageSize),
          currentPage: params.page,
        };
      } else if (response.content) {
        // Spring Data JPA 格式
        return {
          items: response.content || [],
          total: response.totalElements || 0,
          totalPages: response.totalPages || 0,
          currentPage: response.number + 1 || 1,
        };
      } else if (response.items) {
        // 自定义分页格式
        return {
          items: response.items || [],
          total: response.total || 0,
          totalPages: response.totalPages || 0,
          currentPage: response.currentPage || 1,
        };
      } else {
        // 默认处理
        return {
          items: response || [],
          total: (response || []).length,
          totalPages: Math.ceil((response || []).length / params.pageSize),
          currentPage: params.page,
        };
      }
    } catch (error) {
      console.error("Error fetching job applications:", error);
      throw new Error("获取求职申请列表失败");
    }
  },

  /**
   * 获取单个求职申请详情
   * @param {number} id - 求职申请ID
   * @returns {Promise<Object>} - 返回求职申请详情
   */
  async getJobApplication(id) {
    try {
      return await get(`job-applications/${id}`);
    } catch (error) {
      console.error("Error fetching job application:", error);
      throw new Error("获取求职申请详情失败");
    }
  },

  /**
   * 创建新的求职申请
   * @param {Object} data - 求职申请数据
   * @returns {Promise<Object>} - 返回创建的求职申请
   */
  async createJobApplication(data) {
    try {
      return await post("job-applications", data, true);
    } catch (error) {
      console.error("Error creating job application:", error);
      throw new Error("创建求职申请失败");
    }
  },

  /**
   * 更新求职申请状态
   * @param {number} id - 求职申请ID
   * @param {string} status - 新状态
   * @returns {Promise<Object>} - 返回更新后的求职申请
   */
  async updateJobApplication(id, data) {
    try {
      return await put(`job-applications/${id}`, data, true);
    } catch (error) {
      console.error("Error updating job application:", error);
      throw new Error("更新求职申请失败");
    }
  },

  /**
   * 删除求职申请
   * @param {number} id - 求职申请ID
   * @returns {Promise<void>}
   */
  async deleteJobApplication(id) {
    try {
      await del(`job-applications/${id}`);
    } catch (error) {
      console.error("Error deleting job application:", error);
      throw new Error("删除求职申请失败");
    }
  },

  /**
   * 批量删除求职申请
   * @param {number[]} ids - 求职申请ID数组
   * @returns {Promise<void>}
   */
  async batchDeleteJobApplications(ids) {
    try {
      await post("job-applications/batch-delete", { ids }, true);
    } catch (error) {
      console.error("Error batch deleting job applications:", error);
      throw new Error("批量删除求职申请失败");
    }
  },

  /**
   * 根据求职者ID获取其所有求职申请
   * @param {number} jobSeekerId - 求职者ID
   * @returns {Promise<Array>} - 返回求职申请列表
   */
  async getJobApplicationsByJobSeeker(jobSeekerId) {
    try {
      return await get(`job-applications/job-seeker/${jobSeekerId}`);
    } catch (error) {
      console.error("Error fetching job seeker applications:", error);
      throw new Error("获取求职者申请列表失败");
    }
  },

  /**
   * 根据企业职位ID获取所有求职申请
   * @param {number} companyJobId - 企业职位ID
   * @returns {Promise<Array>} - 返回求职申请列表
   */
  async getJobApplicationsByCompanyJob(companyJobId) {
    try {
      return await get(`job-applications/company-job/${companyJobId}`);
    } catch (error) {
      console.error("Error fetching company job applications:", error);
      throw new Error("获取职位申请列表失败");
    }
  },

  /**
   * 更新求职申请状态
   * @param {number} id - 求职申请ID
   * @param {string} status - 新状态
   * @returns {Promise<Object>} - 返回更新后的求职申请
   */
  async updateApplicationStatus(id, status) {
    try {
      // 将状态作为URL参数发送
      return await put(
        `job-applications/${id}/status?status=${encodeURIComponent(status)}`,
        {},
        true
      );
    } catch (error) {
      console.error("Error updating application status:", error);
      throw new Error("更新申请状态失败");
    }
  },
};
